package com.kvc.extend.papervision3d 
{
	import gs.TweenMax;

	import de.polygonal.ds.DLinkedList;
	import de.polygonal.ds.DListNode;

	import org.papervision3d.core.proto.MaterialObject3D;
	import org.papervision3d.events.FileLoadEvent;
	import org.papervision3d.materials.*;
	import org.papervision3d.materials.utils.BitmapMaterialTools;
	import org.papervision3d.objects.DisplayObject3D;

	import flash.events.Event;
	/**
	 * @author Kevin Cao
	 */
	public class Book3D extends DisplayObject3D 
	{
		/*
		 * Global Settings
		 */
		public static const TIME : Number = 1.5;		public static const PRECISE : Boolean = false;		public static const DEFAULT_WIDTH : Number = 600;		public static const DEFAULT_HEIGHT : Number = 800;
		public static const SEGMENTS_W : int = 6;		public static const SEGMENTS_H : int = 4;

		
		private var data : XML;

		private var pages : DLinkedList;
		private var currentNode : DListNode;

		private var eob : Boolean = false; // end of book

		public function get currentPage() : Page3D
		{
			return currentNode.data as Page3D;
		}

		public function Book3D(xml : XML) 
		{
			data = xml;
			pages = new DLinkedList();

			buildBook();
		}

		private function buildBook() : void
		{
			var width : Number = data.@width;
			var height : Number = data.@height;
			
			if(!width) width = DEFAULT_WIDTH;
			if(!height) height = DEFAULT_HEIGHT;
			
			var mat1 : MaterialObject3D;
			var mat2 : MaterialObject3D;

			var page : Page3D;
			var length : int = data.page.length();
			for (var i : int = 0;i < length; i++) 
			{
				mat1 = new BitmapFileMaterial(data.page[i].front, PRECISE);
				mat2 = new BitmapFileMaterial(data.page[i].back, PRECISE);

				// 背面水平镜像
				mat2.addEventListener(FileLoadEvent.LOAD_COMPLETE, mirrorBitmap);
				
				mat1.smooth = mat2.smooth = true;
								page = new Page3D(i, mat1, mat2, width, height);
				page.addEventListener(Event.COMPLETE, showHidePages);
				addChild(page);
				
				pages.append(page);
			}
			
			currentNode = pages.head;
			showHidePages();
		}

		public function pageDown() : void 
		{
			currentPage.visible = true;
			
			if(currentNode.next) 
			{
				currentPage.pageDown();
				currentNode = currentNode.next;
				currentPage.visible = true;
			} else {
				if(!eob) 
				{
					eob = true;
					currentPage.pageDown();
				}
			}
		}

		public function pageUp() : void 
		{
			currentPage.visible = true;
			
			if(eob) 
			{
				currentPage.pageUp();
				if(currentNode.prev) 
				{
					currentNode.prev.data.visible = true;
				}
				eob = false;
			} else {
				if(currentNode.prev) 
				{
					currentNode = currentNode.prev;
					currentPage.visible = true;
					if(currentNode.prev) 
					{
						currentNode.prev.data.visible = true;
					}
					currentPage.pageUp();
				}
			}
		}

		private function showHidePages(event : Event = null) : void
		{
			// 如果还有动画，则跳过此次更新显示状态。用于解决动画重叠问题。to be fixed.
			if(TweenMax.getAllTweens().length > 0) return;
			
			var node : DListNode = pages.head;
			while(node) 
			{
				if(node == currentNode) 
				{
					node.data.visible = true;
				} else {
					if(node == currentNode.prev && eob == false) 
					{
						node.data.visible = true;
					} else {
						node.data.visible = false;
					}
				}
				node = node.next;
			}
		}

		private function mirrorBitmap(event : FileLoadEvent) : void
		{
			BitmapMaterialTools.mirrorBitmapX((event.target as BitmapMaterial).bitmap);
		}
	}
}
